<?php

class Apporder_model extends Base_Model
{
    Const TABLE = 'app_order';

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    /**
     * 获取信息
     *
     * @param $data
     * @return mixed
     */
    public function orderlist($data, $offset, $limit)
    {
        $this->db->select(
                          'id,
                           title,
                           startDay,
                           stopDay,
                           persons,
                           moneyTotalRMB,
                           status'
        );
        $this->db->from(self::TABLE)->where($data)->order_by('created');
        $this->db->limit($limit, $offset);

        $data = $this->db->get()->result_array();
        if($data){
            foreach($data as $key => &$val){
                $val['status'] = self::orderStatusName($val['status']);
                $val['moneyTotalRMB']=$val['moneyTotalRMB']/100;
                $val['moneyTotalRMB']='￥'.$val['moneyTotalRMB'];
            }
        }
        return $data;
    }

    /**
     * 处理列表
     *
     * @param $where
     * @return mixed
     */
    function getDealOrder($where)
    {
        $sql = <<<EOT
SELECT
	a.id as order_id,
	a.startDay,
	a.stopDay,
    a.persons,
	a.moneyTotalRMB,
	a.status,
	c.name as orderName,
	c.thumb
FROM
	app_order a
LEFT JOIN (
	SELECT
		id,
		uid,
		name,
		thumb,
		type
	FROM
		mod_activity
	UNION ALL
		SELECT
			id,
			uid,
			name,
			thumb,
			type
		FROM
			mod_track
	UNION ALL
		SELECT
			id,
			uid,
			name,
			thumb,
			type
		FROM
			mod_ziying
) c ON c.id = a.goodsId
AND c.type = a.goodsType WHERE
EOT;
        $sql .= $where;
        $data=$this->db->query($sql)->result_array();
        if($data){
        	foreach($data as $key => &$val){
        		$val['status'] = self::orderStatusName($val['status']);
        		$val['moneyTotalRMB']=$val['moneyTotalRMB']/100;
        		$val['moneyTotalRMB']='￥'.$val['moneyTotalRMB'];
        	}
        }
        return $data;
    }
    /**
     * 处理预定的订单详情
     *
     * @param $data
     * @return mixed
     */
    public function dealOrderDetail($data)
    {
    	$this->db->select(
    			'ao.id as order_id,
                           ao.title,
                           ao.startDay,
                           ao.stopDay,
                           ao.persons,
                           ao.moneyTotalRMB,
                           ao.status,
                           ao.orderNo,
                           ao.created,
                           ao.uid,
    			           ao.commentsSaler,
                           u.telNum,
                           u.telCode,
                           u.userName,
                           u.email'
    	);
    	$this->db->from(self::TABLE.' as ao')->where($data);
    	$this->db->join('user_v2 as u', 'u.uid = ao.uid', 'left');
    	$this->db->where($data);
    
    	$data = $this->db->get()->row_array();
    	if($data){
    		$data['status_id'] = $data['status'];
    		//与下方顶部状态title字段重复，重命名
    		$data['orderName']= $data['title'];
    		//订单详情进度条
    		$data['process'] = self::orderProcessName($data['status']);
    		//订单详情顶部状态栏
    		$top = self::dealOrderStatusName($data);
    		$data = array_merge($data, $top);
    		//订单状态名
    		$data['status'] = self::orderStatusName($data['status']);
    		//人民币价格
    		$data['moneyTotalRMB']=$data['moneyTotalRMB']/100;
    		$data['moneyTotalRMB']='￥'.$data['moneyTotalRMB'];
    		$data['created'] = date('Y-m-d H:i:s', $data['created']);
		    unset($data['commentsSaler']);
		}
    
    	return $data;
    }

    /**
     * 我的订单中订单详情
     *
     * @param $data
     * @return mixed
     */
    public function orderDetail($data)
    {
        $this->db->select(
                          'ao.id as order_id,
                           ao.title,
                           ao.startDay,
                           ao.stopDay,
                           ao.persons,
                           ao.moneyTotalRMB,
                           ao.status,
                           ao.orderNo,
                           ao.created,
                           ao.shopId,
        		           ao.commentsSaler,
        		           ao.uid,
                           u.telNum,
                           u.telCode,
                           u.userName,
                           u.email'
        );
        $this->db->from(self::TABLE.' as ao')->where($data);
        $this->db->join('user_v2 as u', 'u.uid = ao.shopId', 'left');
        $this->db->where($data);

        $data = $this->db->get()->row_array();
        if($data){
            $data['status_id'] = $data['status'];
            //与下方顶部状态title字段重复，重命名
            $data['orderName']= $data['title'];
            //订单详情进度条
            $data['process'] = self::orderProcessName($data['status']);                    
            //订单详情顶部状态栏
            $top = self::dealOrderStatusName($data);
            $data = array_merge($data, $top);
            //订单状态名
            $data['status'] = self::orderStatusName($data['status']);
            //人民币价格
            $data['moneyTotalRMB']=$data['moneyTotalRMB']/100;
            $data['moneyTotalRMB']='￥'.$data['moneyTotalRMB'];
            $data['created'] = date('Y-m-d H:i:s', $data['created']);
		    unset($data['commentsSaler']);
		}

        return $data;
    }

    /**
     * 更新订单
     *
     * @param $data
     * @param $where
     * @return CI_DB_active_record|CI_DB_result
     */
    public function updateOrder($data, $where)
    {
        $this->db->set($data);
        $this->db->where($where);
        return $this->db->update(self::TABLE);
    }


    /**
     * 订单状态
     *
     * @param $statusId
     * @return string
     */
    public static function orderStatusName ($statusId)
    {
        switch ($statusId)
        {
            case 0:
                return '等待付款';
                break;

            // 已付款
            case 1:
                return '等待确认';
                break;

            case 2:
                return '玩伴拒绝';
                break;

            // 导游同意后
            case 3:
                return '行程中';
                break;

            // 退款中
            case 31:
                return '退款中';
                break;

            // 行程完成
            case 99:
                return '待评价';
                break;

            // 退款流程
            case 4:
                return '退款中';
                break;

            case 5:
                return '退款中';
                break;

            case 6:
                return '已退款';
                break;

            case 32:
                return '已退款';
                break;
            // 作废流程
            case 7:
                return '已完成';
                break;
        }
    }
    
    /**
     * 订单进度 0：无进度 1:付款 2：订单确认 3：行程中 4：完成
     *
     * @param $statusId
     * @return int
     */
    public static function orderProcessName ($statusId)
    {
    	switch ($statusId)
    	{
    		case 0:
    			return '0';//'等待付款'
    			break;
    
    			// 已付款
    		case 1:
    			return '1';//'等待确认'
    			break;   
    
    			// 导游同意后
    		case 3:
    			return '3';//'行程中'
    			break;
    
    			// 退款申请
    		case 31:
    			return '3';//'退款中'
    			break;
    		
    		case 32:
    			return '3';//'已退款';
    			break;
    			
    			// 行程完成
    		case 99:
    			return '4';//'待评价';
    			break;
    
    			// 玩伴拒绝
    		case 2:
    			return '2';//'玩伴拒绝'
    			break;
    			
    		case 4:
    			return '2';//'退款中';
    			break;
    
    		case 5:
    			return '2';//'退款中'
    			break;
    
    		case 6:
    			return '4';//'已退款'
    			break;
    
    			// 点评完毕
    		case 7:
    			return '4';//'已完成'
    			break;
    	}
    }
    /**
     * 订单状态
     *
     * @param $statusId
     * @return string
     */
    public static function dealOrderStatusName ($orderInfo)
    {
    	$statusId=$orderInfo['status_id'];
    	$commentsSaler=$orderInfo['commentsSaler'];
    	$startDay=$orderInfo['startDay'];
    	$stopDay=$orderInfo['stopDay'];
    	
        switch ($statusId)
        {
            case 0:
                return array('title' => '等待付款', 'content' => '');
                break;

            // 已付款
            case 1:
                return array('title' => '付款成功', 'content' => '等待订单确认');
                break;
            
            //玩伴拒绝
            case 2:
                return array('title' => '预定失败', 'content' => '玩伴拒绝了您的预定申请，理由为:《'.$commentsSaler.'》，哇吖会将费用原路退回您的支付宝或微信');
                break;
            case 4:
               	return array('title' => '退款中', 'content' => '玩伴拒绝了您的预定申请，理由为:《'.$commentsSaler.'》，哇吖会将费用原路退回您的支付宝或微信');
               	break;
                
            case 5:
               	return array('title' => '退款中', 'content' => '玩伴拒绝了您的预定申请，理由为:《'.$commentsSaler.'》，哇吖会将费用原路退回您的支付宝或微信');
               	break;

            case 6:
               	return array('title' => '已退款', 'content' => '哇吖已成功将费用退回你的支付宝或微信');
               	break;
            
            // 玩伴同意后
            case 3:
                return array('title' => '订单已确认', 'content' => '请牢记您的出行日期:'.$startDay.'至'.$stopDay);
                break;

            // 退款申请
            case 31:
                return array('title' => '退款申请开始受理', 'content' => '哇吖已受理退款申请，并会于1个工作日内与您取得联系');
                break;
            
            case 32:
                return array('title' => '退款申请受理完毕', 'content' => '');
                break;

            // 待评价
            case 99:
                return array('title' => '待评价', 'content' => '');
                break;
            
            case 7:
                return array('title' => '已完成', 'content' => '');
                break;

            default:
                return array('title' => '', 'content' => '');
                break;
        }
    }
    /**
     * 获取行程状态错误
     */
    public function ordererror($type,$statusId)
    {
    	switch ($type)
    	{
    		case 'FINISH':
    			if (in_array($statusId, array(0,1,2,4,5,6))) {
    				return "行程还未开始，无法完成行程";
    			}
    			elseif (in_array($statusId, array(7,99))){
    				return "行程已经完成";
    			}
    			elseif ($statusId==31)
    			    return "退款申请还未处理完毕，无法完成行程";
    			else{
    				return "系统错误";
    			}
    		case 'SHENSU':
    			if (in_array($statusId, array(0,1,2,4,5,6))) {
    				return "行程还未开始，无法发起退款申请";
    			}
    			elseif (in_array($statusId, array(7,99))){
    				return "行程已经完成，无法发起退款申请";
    			}
    			elseif (in_array($statusId, array(31,32)))
    			return "退款申请正在受理中";
    			else{
    				return "系统错误";
    			}
    			break;
    	    default:
    	    	return '';
    	}
    }

    public function getOrderBy($data)
    {
        $this->db->select('id,orderNo,uid,moneyTotalRMB,title,shopId,goodsType,goodsId,persons');
        $this->db->from(self::TABLE)->where($data);
		$data = $this->db->get();
		$cou=$this->db->count_all_results();
		if($cou==1)
			$data = $data->row_array();
		else 
			$data = $data->result_array();
        return $data;
    }
    
    public function addNotice($send,$receive,$message)
    {
    	$data = array(
    			'sendUser'=>$send,
    			'receiveUser'=>$receive,
    			'type'=>0,
    			'message'=>$message,
    			'created'=>time(),
    	);
    	return $this->db->insert('mod_notice', $data);
    }
    public function sendEmail($sender,$to,$subject,$body)
	{
 		$this->load->library('PHPEmail');

		return $this->phpemail->to($to)
			->sender($sender)
			->subject($subject)
			->message($body)
			->send();
    }
}
